package com.iwhalecloud.bss.kite.cucc.service.order;

import com.alibaba.fastjson.JSON;
import com.iwhalecloud.bss.kite.cart.common.constant.KiteCartErrorConsts;
import com.iwhalecloud.bss.kite.cart.common.util.KiteCartListUtils;
import com.iwhalecloud.bss.kite.cart.common.util.KiteCartMapUtils;
import com.iwhalecloud.bss.kite.cart.common.util.KiteCartStringUtils;
import com.iwhalecloud.bss.kite.cart.service.callclient.constant.CallClientEnum;
import com.iwhalecloud.bss.kite.common.util.KiteBeanUtils;
import com.iwhalecloud.bss.kite.common.util.KiteStringUtils;
import com.iwhalecloud.bss.kite.cucc.client.api.inf.IInfServiceBus;
import com.iwhalecloud.bss.kite.cucc.client.api.order.IDiscountInfoService;
import com.iwhalecloud.bss.kite.cucc.client.dto.order.KiteCartFindoopOrderReqDTO;
import com.iwhalecloud.bss.kite.cucc.client.dto.order.KiteCartFindoopOrderRspDTO;
import com.iwhalecloud.bss.kite.cucc.client.dto.order.KiteFindoopOrderDTO;
import com.iwhalecloud.bss.kite.cucc.client.vo.scene.DiscountVO;
import com.iwhalecloud.bss.kite.cucc.manager.scene.service.operate.LocalOperateOfferInstDiscountService;
import com.iwhalecloud.bss.kite.manager.inst.KiteOfferInstDiscount;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service
public class DiscountInfoService implements IDiscountInfoService {

    @Autowired
    private LocalOperateOfferInstDiscountService operateOfferInstDiscountService;
    @Autowired
    IInfServiceBus infServiceBus;

    @Override
    public List<DiscountVO> addDiscountInfo(String sceneInstId, String custId, String offerInstId, String discountsJson) {
        List<DiscountVO> discountVOs = new ArrayList<DiscountVO>();
        List<KiteOfferInstDiscount> kiteOfferInstDiscount = operateOfferInstDiscountService.addOfferInstDiscount(
            sceneInstId, custId, offerInstId, discountsJson);

        kiteOfferInstDiscount.stream()
            .forEach(discount -> discountVOs.add(KiteBeanUtils.transform(discount, DiscountVO.class)));
        return discountVOs;
    }

    @Override
    public List<DiscountVO> deleteDiscountInfo(String sceneInstId, String custId, String offerInstId, String discountInstId) {
        List<KiteOfferInstDiscount> kiteOfferInstDiscount = operateOfferInstDiscountService
            .deleteOfferInstDiscount(sceneInstId, custId, offerInstId, discountInstId);
        List<DiscountVO> discountVOs = new ArrayList<DiscountVO>();

        kiteOfferInstDiscount.stream()
            .forEach(discount -> discountVOs.add(KiteBeanUtils.transform(discount, DiscountVO.class)));

        return discountVOs;
    }


    public List<DiscountVO> entryDiscountInfo(String sceneInstId, String custId, String offerInstId, String discountJson) {
        List<DiscountVO> discountVOs = new ArrayList<DiscountVO>();
        List<KiteOfferInstDiscount> kiteOfferInstDiscount = operateOfferInstDiscountService.entryOfferInstDiscount(sceneInstId, custId, offerInstId, discountJson);

        kiteOfferInstDiscount.stream()
            .forEach(discount -> {
                DiscountVO discountVO = KiteBeanUtils.transform(discount, DiscountVO.class);
                if (KiteStringUtils.isEmpty(discountVO.getDiscountInstId())) {
                    discountVO.setDiscountInstId(discount.getOfferObjInstRelId());
                }
                discountVOs.add(discountVO);
            });
        return discountVOs;
    }
    public List<DiscountVO> finDoopentryDiscountInfo(String sceneInstId, String custId, String offerInstId, String discountJson) {
        List<DiscountVO> discountVOs = new ArrayList<DiscountVO>();
        List<KiteOfferInstDiscount> kiteOfferInstDiscount = operateOfferInstDiscountService.finDoopentryOfferInstDiscount(sceneInstId, custId, offerInstId, discountJson);
/*
        kiteOfferInstDiscount.stream()
            .forEach(discount -> {
                DiscountVO discountVO = KiteBeanUtils.transform(discount, DiscountVO.class);
                if (KiteStringUtils.isEmpty(discountVO.getDiscountInstId())) {
                    discountVO.setDiscountInstId(discount.getOfferObjInstRelId());
                }
                discountVOs.add(discountVO);
            });*/
        return discountVOs;
    }
    @Override
    public List<DiscountVO> getDiscountInfoDetails(String sceneInstId, String custId, String offerInstId, String discountInstId){
        List<DiscountVO> discountVOs = new ArrayList<DiscountVO>();
        List<KiteOfferInstDiscount> kiteOfferInstDiscount = operateOfferInstDiscountService.listOfferInstDiscounts(sceneInstId, custId, offerInstId);

        kiteOfferInstDiscount.stream()
        .forEach(discount -> discountVOs.add(KiteBeanUtils.transform(discount, DiscountVO.class)));

        return discountVOs;
    }

    @Override
    public KiteCartFindoopOrderRspDTO queryFindoopOrder(KiteCartFindoopOrderReqDTO reqDTO) {
        String result = infServiceBus.findoopOrderList(reqDTO);
        if (KiteCartStringUtils.isBlank(result)) {
            KiteCartErrorConsts.INF_INVOKE_ERROR.throwOut(CallClientEnum.INF_COMMON_URL.getServiceCode(), "查询失败", "");
        }
        KiteCartFindoopOrderRspDTO infoRspBody = new KiteCartFindoopOrderRspDTO();

        Map<String, Object> goodsMap = JSON.parseObject(result, Map.class);
        Map<String, Object> resultBodyMap = (Map<String, Object>) goodsMap.get("UNI_BSS_BODY");
        Map<String, Object> findOOPOrderListRsp = (Map<String, Object>) resultBodyMap
            .get("queryListRsp");
        if ("10000".equals(KiteCartMapUtils.getString(findOOPOrderListRsp, "code"))) {
            infoRspBody = this.dealFindoopOrderResp(findOOPOrderListRsp);

//            findoopOrderDTOS = this.dealFindoopOrderResp((List<Map<String, Object>>) RSP_DETAIL.get("data"));
            infoRspBody.setResultCode(KiteCartMapUtils.getString(findOOPOrderListRsp, "code"));
            infoRspBody.setResultMsg(KiteCartMapUtils.getString(findOOPOrderListRsp, "message"));
        }
        else {
            findOOPOrderListRsp = (Map<String, Object>) goodsMap
                .get("UNI_BSS_HEAD");
            infoRspBody.setResultCode("9999");
            infoRspBody.setResultMsg("查询oop折扣申请单失败：" + KiteCartMapUtils.getString(findOOPOrderListRsp, "message"));
        }
        return infoRspBody;
    }

    private KiteCartFindoopOrderRspDTO dealFindoopOrderResp(Map<String, Object> findOOPOrderListRsp) {
        KiteCartFindoopOrderRspDTO rspDTO=new KiteCartFindoopOrderRspDTO();

        Map<String,Object> dataMap=KiteCartMapUtils.getMap(findOOPOrderListRsp,"data");
        String pageNo=KiteCartMapUtils.getString(dataMap,"pageNo");
        String pageSize=KiteCartMapUtils.getString(dataMap,"pageSize");
        rspDTO.setPageNo(pageNo);
        rspDTO.setPageSize(pageSize);
        List<Map<String, Object>> lists= (List<Map<String, Object>>) dataMap.get("list");
        List<KiteFindoopOrderDTO> resultList=new ArrayList<>();
        if(KiteCartListUtils.isNotEmpty(lists)){
            for (Map<String, Object> map : lists) {
                KiteFindoopOrderDTO orderRspDTO =new KiteFindoopOrderDTO();
                orderRspDTO.setApplyName(KiteCartMapUtils.getString(map,"applyName"));
                orderRspDTO.setCustName(KiteCartMapUtils.getString(map,"custName"));
                orderRspDTO.setGoodsName(KiteCartMapUtils.getString(map,"goodsName"));
                orderRspDTO.setApplyCode(KiteCartMapUtils.getString(map,"applyCode"));
                orderRspDTO.setApplyType(KiteCartMapUtils.getString(map,"applyType"));
                orderRspDTO.setOopCode(KiteCartMapUtils.getString(map,"oopCode"));
                orderRspDTO.setCreateTime(KiteCartMapUtils.getString(map,"createTime"));
                orderRspDTO.setFinishTime(KiteCartMapUtils.getString(map,"finishTime"));
                orderRspDTO.setProvinceCode(KiteCartMapUtils.getString(map,"provinceCode"));
                orderRspDTO.setCreatorLogin(KiteCartMapUtils.getString(map,"creatorLogin"));
                orderRspDTO.setDetailUrl(KiteCartMapUtils.getString(map,"detailUrl"));
                orderRspDTO.setCustId(KiteCartMapUtils.getString(map,"custId"));
                orderRspDTO.setId(KiteCartMapUtils.getString(map,"id"));
                orderRspDTO.setStatus(KiteCartMapUtils.getString(map,"status"));
                resultList.add(orderRspDTO);
            }
        }
        rspDTO.setResultObject(resultList);
        return rspDTO;
    }
}
